$(document).ready(function () {
  var hulla = new hullabaloo()
  var RSUId = 0
  /**
   * @description: 预加载
   * @param {*}
   * @return {*}
   */
  $.ajax({
    url: '../../rsu/getRsuList', //引入地址
    type: 'GET', //请求方式
    success: function (response) {
      // console.log(response.data.rsu[0])
      var html = ''
      for (var i = 0; i < response.data.rsu.length; i++) {
        html += `<tr>
            <td>${response.data.rsu[i].rsuId}</td> 
            <td>${response.data.rsu[i].rsuLocation}</td> 
             <td><button id='${response.data.rsu[i].rsuId}' onclick = 'showRSUCar(this)' type="button" class="btn btn-info">
            查看详情<span class="glyphicon glyphicon-search showdetailSpan" aria-hidden="true"></span>
            </button> </td>
            <td><button id='${response.data.rsu[i].rsuId}' onclick = 'showRSUMsg(this)' type="button" class="btn btn-info">
            查看详情<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
            </button> </td>
            <td><button id='${response.data.rsu[i].rsuId}' onclick = 'deleteRow(this)' type="button" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
            </button> 
            </td>
            </tr>`
      }
      $('#RSUTableT').append(html)
    },
    error: function () {
      //请求失败的回调方法
      //   var html = "";
      //   html += "<tr><td>数据请求失败</td></tr>";
      //   $('#RSUTable').html(html)
      // response = {
      //   data: [{ id: 1, RSUPos: '122.3,222.5' }]
      // }
      var html = ''
      for (var i = 0; i < response.data.rsu.length; i++) {
        html += `<tr>
            <td>${response.data.rsu[i].rsuId}</td> 
            <td>${response.data.rsu[i].rsuLocation}</td> 
             <td><button id='${response.data.rsu[i].rsuId}' onclick = 'showRSUCar(this)' type="button" class="btn btn-info">
            查看详情<span class="glyphicon glyphicon-search showdetailSpan" aria-hidden="true"></span>
            </button> </td>
            <td><button id='${response.data.rsu[i].rsuId}' onclick = 'showRSUMsg(this)' type="button" class="btn btn-info">
            查看详情<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
            </button> </td>
            <td><button id='${response.data.rsu[i].rsuId}' onclick = 'deleteRow(this)' type="button" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
            </button> 
            </td>
            </tr>`
      }
      $('#RSUTableT').append(html)
    }
  })

  /**
   * @description: 添加信息
   * @param {*}
   * @return {*}
   */
  addRSUBtn = function () {
    var RSULocation = $('#inputRSULocation').val()
    if (RSULocation == '') {
      $('#RSULocationForm').addClass('has-error')
      $('#RSULocationIptDiv').append(
        " <span class='help-block'>不可为空</span>"
      )
      setTimeout(() => {
        $('#RSULocationForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (RSULocation != '') {
      $.ajax({
        url: '../../rsu/addRsu', //引入地址
        type: 'POST', //请求方式
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data:JSON.stringify( {
          rsuLocation: RSULocation
        }),
        success: function (response) {
          $('.help-block').remove()
          if (response.success) {
            hulla.send(response.message, 'success')
            window.location.reload()
          }
        },
        error: function (response) {
          hulla.send(response.message, 'danger')
        }
      })
    }
  }

  /**
   * @description: 删除信息
   * @param {*}
   * @return {*}
   */
  deleteRow = function (tr) {
    $.ajax({
      url: '../../rsu/removersu',
      type: 'POST',
      data: {
        RsuId: tr.id
      },
      success: function (response) {
        hulla.send(response.message, 'success')
        window.location.reload()
      },
      error: function (error) {
        hulla.send(response.message, 'danger')
      }
    })
  }

  showRSUCar = function (tr) {
    RSUId = tr.id;
    var storage=window.localStorage;
    storage.RSUId=tr.id;
    $.ajax({
      url: '../../rsu/getCarInside/'+RSUId,
      type: 'GET',
      // data: {
      //   // RsuId: RSUId
      // },
      success: function (response) {
        window.location.href="../html/carInfo.html";
      },
      error: function (error) {
        window.location.href="../html/carInfo.html";
      }
    })
  }
  showRSUMsg = function (tr) {
     RSUId = tr.id;
    var storage=window.localStorage;
    storage.RSUId=tr.id;
    $.ajax({
      url: '../../rsu/getRsuMsg/'+RSUId,
      type: 'GET', //GET
      // data: {
      //   RSUId: RSUId
      // },
      success: function (response) {
        window.location.href="../html/RSUMsg.html";
      },
      error: function (error) {
        window.location.href="../html/RSUMsg.html";
      }
    })
  }
})
